package com.wtf.dsp.admin.controller;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
import com.wtf.dsp.admin.model.dto.*;
import com.wtf.dsp.admin.model.vo.DsBaseInfoVO;
import com.wtf.dsp.admin.model.vo.DsCloneVO;
import com.wtf.dsp.admin.model.vo.DsVO;
import com.wtf.dsp.admin.service.DataSourceService;
import com.wtf.dsp.core.api.R;
import com.wtf.dsp.core.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "数据源")
@RestController
@RequestMapping("/ds")
public class DataSourceController {

    @Autowired
    private DataSourceService dataSourceService;

    @ApiOperation(value = "获取数据源(分页)")
    @GetMapping("/page")
    public R<PageUtils> page(@Validated DsPageDTO dsPageDTO){
        PageUtils pageUtils = dataSourceService.page(dsPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取数据源")
    @GetMapping("/info")
    public R<DsVO> info(@Validated DsListDTO dsListDTO){
        DsVO dsVO = dataSourceService.info(dsListDTO);
        return R.ok(dsVO);
    }

    @ApiOperation(value = "获取数据源")
    @GetMapping("/list")
    public R<List<DsBaseInfoVO>> list(@Validated DsListDTO dsListDTO){
        List<DsBaseInfoVO> dataSources = dataSourceService.list(dsListDTO).stream().map(e->{
            DsBaseInfoVO baseInfoVO = new DsBaseInfoVO();
            baseInfoVO.setNs(e.getNs());
            baseInfoVO.setDs(e.getDs());
            baseInfoVO.setDbName(e.getDbName());
            return baseInfoVO;
        }).collect(Collectors.toList());
        return R.ok(dataSources);
    }

    @ApiOperation(value = "新增数据源")
    @PostMapping("/add")
    public R add(@RequestBody @Validated DsAddDTO dsAddDTO){
        int save = dataSourceService.add(dsAddDTO);
        return R.ok(save);
    }

    @ApiOperation(value = "更新数据源")
    @PostMapping("/update")
    public R update(@RequestBody @Validated DsUpdateDTO dsUpdateDTO){
        int update = dataSourceService.update(dsUpdateDTO);
        return R.ok(update);
    }

    @ApiOperation(value = "克隆数据")
    @PostMapping("/clone")
    public R clone(@RequestBody @Validated DsCloneDTO dsCloneDTO){
        DsCloneVO result = dataSourceService.clone(dsCloneDTO);
        return R.ok(result);
    }

    @ApiOperation(value = "删除数据源")
    @PostMapping("/del")
    public R remove(@RequestBody @Validated DsDelDTO dsDelDTO){
        int del = dataSourceService.remove(dsDelDTO);
        return R.ok(del);
    }

    @ApiOperation(value = "测试连接")
    @PostMapping("/testConnect")
    public R testConnect(@RequestBody @Validated DsAddDTO dsAddDTO) throws Exception {
        Boolean tesResult = dataSourceService.testConnect(dsAddDTO);
        return tesResult ? R.ok() : R.error("连接失败，请检查主机、账号、密码、数据库、网络是否正确");
    }
}
